﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

namespace MLY.LTR.Clients
{
    public partial class SubContractors : Bll.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
            lblErrDelete.Visible = false;
            lblSuccessDelete.Visible = false;
        }

        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (ObjectDataSource1.SelectParameters[1].DefaultValue == null ||
            ObjectDataSource1.SelectParameters[2].DefaultValue == null ||
            ObjectDataSource1.SelectParameters[1].DefaultValue !=
            e.SortExpression ||     ObjectDataSource1.SelectParameters[2].DefaultValue == "Desc")
                { //sort direction
                    ObjectDataSource1.SelectParameters[2].DefaultValue = "Asce";
                }
                else if (ObjectDataSource1.SelectParameters[2].DefaultValue == "Asce")
                {
                    ObjectDataSource1.SelectParameters[2].DefaultValue = "Desc";

                }

                //Which column to sort on.
                ObjectDataSource1.SelectParameters[1].DefaultValue = e.SortExpression;

                //We have to do this organizationRelation we will get an exception
                GridView1.DataBind();
                e.Cancel = true;

        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        protected void btnDelete_OnClick(object sender, EventArgs e)
        {
            int Contractor = Int32.Parse(((ImageButton)sender).CommandArgument);
            
            if (Bll.clsOrganizationRelation.DeleteRelation(SessParam.OrganizationId, Contractor))
            {

                GridView1.DataBind();
                lblErrDelete.Visible = false;
                lblSuccessDelete.Visible = true;          
            }
            else
            {
                lblErrDelete.Visible = true;
                lblSuccessDelete.Visible = false;
            }
        }
        protected void GridView1_PreRender(object sender, EventArgs e)
        {
            if ((GridView1.Rows.Count == 0) && (Session["OrganizationId"] != null))
            {
                divEmptyGrid.Visible = true;
            }
            else
            {
                divEmptyGrid.Visible = false;
            }
        }

        protected void imgAddNewOnNoRows_Click(object sender, ImageClickEventArgs e)
        {
            Response.RedirectTo("SubContractorCard.aspx?ContractorId=-1");
        }
    }
}
